<?php
/*******************************************************************************

    FinalsClub.org is a platform for students and faculty to collaborate
    and share their knowledge with the world.

    Copyright (C) 2009  Andrew Magliozzi
 
    This file is a part of FinalsClub.org.

    FinalsClub.org is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.
 
    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.
 
    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
    To contact FinalsClub.org, send an email to info@finalsclub.org, or
    send mail to Finals Club, c/o Veritas Tutors, 1132 Massachusetts Avenue,
    Cambridge, MA 02138.

*******************************************************************************/

class MessagePeer extends BaseMessagePeer {
	
	const INBOX_FOLDER = 'inbox';
	const SENT_FOLDER = 'sent';
	const TRASH_FOLDER = 'trash';
	const GROUP_TYPE = 'group';
	const USER_TYPE = 'user';	
	
	/**
	 * Retrieve a message object for a given primary id key
	 * @param $id
	 * @param $user_id (optional)
	 * @return a single Message object
	 */
	public static function retrieveById($id, $user_id = null) {
		if($user_id) {
			$c = new Criteria();
			$c->add(MessagePeer::USER_ID, $user_id);
			$c->add(MessagePeer::ID, $id);
			return MessagePeer::doSelectOne($c);
		} else {
			return MessagePeer::retrieveByPK($id);
		}
	}
	

	/**
	 * Retrieve all messages for a given group for a given user
	 * @param $group_id
	 * @param $folder
	 * @param $page
	 * @param $per_page
	 * @return sfPager object with results containing an array of Message objects
	 */
	public static function retrieveByGroupIdPager($group_id, $user_id, $folder = MessagePeer::INBOX_FOLDER, $page = 1, $per_page = 20) {
		$c = new Criteria();
		$c->add(MessagePeer::TYPE, 'group');
		$c->add(MessagePeer::TYPE_ID, $group_id);	
		$c->add(MessagePeer::USER_ID, $user_id);
		
		if($folder == MessagePeer::TRASH_FOLDER) {
			$c->add(MessagePeer::TRASHED, true);
		} else {
			$c->add(MessagePeer::FOLDER, $folder);
		}
		$c->addDescendingOrderByColumn(MessagePeer::CREATED_AT);
		$pager = new sfPropelPager('Message', $per_page);
		$pager->setPage($page);
		$pager->setCriteria($c);
		if($folder == MessagePeer::INBOX_FOLDER) {
			$pager->setPeerMethod('doSelectJoinUserRelatedBySenderId');			
		} else {
			$pager->setPeerMethod('doSelectJoinUserRelatedByRecipientId');
		}
		$pager->init();
		return $pager;
	}
	
	
	/**
	 * Retrieve all messages for a given user
	 * @param $user_id
	 * @param $folder
	 * @param $page
	 * @param $per_page
	 * @return pager
	 */
	public static function retrieveByRecipientIdPager($user_id, $folder = 'inbox', $page = 1, $per_page = 20) {
		$c = new Criteria();
		$c->add(MessagePeer::RECIPIENT_ID, $user_id);
		
		if($folder == MessagePeer::TRASH_FOLDER) {
			$c->add(MessagePeer::TRASHED, true);
		} else {
			$c->add(MessagePeer::FOLDER, $folder);
		}
		$c->addDescendingOrderByColumn(MessagePeer::CREATED_AT);
		$pager = new sfPropelPager('Message', $per_page);
		$pager->setPage($page);
		$pager->setCriteria($c);
		if($folder == MessagePeer::INBOX_FOLDER) {
			$pager->setPeerMethod('doSelectJoinUserRelatedBySenderId');			
		} else {
			$pager->setPeerMethod('doSelectJoinUserRelatedByRecipientId');
		}
		$pager->init();
		return $pager;		
	}

	
	public static function getById($id){
		return MessagePeer::retrieveByPK($id);
	}
	
	
	public static function getInbox($uid,$sidebar = false,$paginated = false ){
		if($sidebar){
			$limit = 5;
			$unseen_only = true;
		}
		else{
			$limit = 0;
			$unseen_only = false;

		}
		return MessagePeer::getFolder($uid,$limit, 'inbox', $unseen_only, $paginated);
	} 	
	
	//(originally) intended to pull up # for inbox counter on the profile/show page
	public static function getUnseenInboxCount($user_id, $folder = 'inbox', $group_id = null){
		$c = new Criteria();
		$c->add(MessagePeer::RECIPIENT_ID, $user_id);
		$c->add(MessagePeer::SEEN,0);
		if($folder == MessagePeer::TRASH_FOLDER) {
			$c->add(MessagePeer::TRASHED, true);
		} else {
			$c->add(MessagePeer::FOLDER, $folder);
		}

		if($group_id)
		{
			$c->add(MessagePeer::TYPE,'group');
			$c->add(MessagePeer::TYPE_ID,$group_id);
		}
		
		return self::doCount($c);
	}
	
	/* This returns a criteria object that specifies a [ {recipeint_id == user_id AND type == user} OR {user->isMember(recipient_id) == True AND type == group} ] logical statement */
	
	/* UPDATE - Given a change in plans, this method is now deprecated... Will be left here until someone feels a need to delete it */
	
	public static function getUserOrGroupCriteria($user){
		$group_cond = new Criteria();
		$group_cond->add($group_cond->getNewCriterion($user->isMember(MessagePeer::RECIPIENT_ID), 1, Criteria::EQUAL));
		$group_cond->add($group_cond->getNewCriterion(MessagePeer::TYPE,"group",Criteria::EQUAL));
		$user_cond = new Criteria();
		$user_cond->add($user_cond->getNewCriterion(MessagePeer::RECIPIENT_ID, $user->getId(), Criteria::EQUAL));
		$user_cond->add($user_cond->getNewCriterion(MessagePeer::TYPE,"user",Criteria::EQUAL));
		return $user_cond->addOr($group_cond);
	}
	
	//retrieve messages for group from most recent to oldest
	public static function retrieveByGroupId($group_id){
		$c = new Criteria();
		$c->add(MessagePeer::TYPE, 'group');
		$c->add(MessagePeer::TYPE_ID, $group_id);
		$c->addDescendingOrderByColumn(MessagePeer::CREATED_AT);
		return MessagePeer::doSelect($c);
	}
	
//retrieve all the messages sent and recieved by user id and the day it was created as a timestamp 
	public static function retrieveByUserIdAndDate($user_id, $time_stamp)
	{
		$c = new Criteria();
		$c->add(MessagePeer::USER_ID, $user_id);
		$c->addOr(MessagePeer::SENDER_ID, $user_id);
		$c->add(MessagePeer::CREATED_AT, date('Y-m-d', $time_stamp), Criteria::GREATER_EQUAL);
		$c->addAnd(MessagePeer::CREATED_AT, date('Y-m-d', $time_stamp + 60*60*24), Criteria::LESS_THAN);
			
 		return MessagePeer::doSelect($c); 		
	
	}
		
}